package com.yidu.hbats.action.AtsBase;

import com.yidu.hbats.dao.AtsBaseItemMapper;
import com.yidu.hbats.model.AtsBaseItem;
import com.yidu.hbats.model.AtsBaseSport;
import com.yidu.hbats.model.AtsBaseSportItem;
import com.yidu.hbats.service.atsBase.AtsBaseItemService;
import com.yidu.hbats.service.atsBase.AtsBaseSportItemService;
import com.yidu.hbats.service.atsBase.AtsBaseSportService;
import com.yidu.utils.Message;
import com.yidu.utils.Page;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.*;

/**
 * @program: spring-boot-demo
 * @description: ${description}
 * @author: clg
 * @create: 2019-11-12 10:02
 */

@Controller
@RequestMapping("AtsBaseSportController")
public class AtsBaseSportController {

    @Resource
    AtsBaseItemService atsBaseItemService;

    @Resource
    AtsBaseSportService atsBaseSportService;

    @Resource
    AtsBaseSportItemService atsBaseSportItemService;

    @Resource
    AtsBaseItemMapper atsBaseItemMapper;

    /**
     * 增加或修改时的下拉框数据
     *
     * @return
     * @throws Exception
     */
    @RequestMapping("/selectXiaLa")
    @ResponseBody
    public List<Map<String, Object>> selectAllItem() {
        List<Map<String, Object>> lsite = new ArrayList<Map<String, Object>>();
        List<AtsBaseItem> list = atsBaseItemService.selectAll2();
        for (Iterator iterator = list.iterator(); iterator.hasNext(); ) {
            AtsBaseItem tmodule = (AtsBaseItem) iterator.next();
            Map<String, Object> map = new HashMap<String, Object>();
            map.put("name", tmodule.getItemExp());
            map.put("pId", tmodule.getParentId());
            map.put("id", tmodule.getItemId());
            lsite.add(map);
        }
        return lsite;
    }


    /**
     * 查询所用和分页
     *
     * @param atsBaseSport
     * @return
     */
    @ResponseBody
    @RequestMapping("selectAll")
    public Map<String, Object> selectAll(AtsBaseSport atsBaseSport, Page page) {

        Map map = new HashMap();
        List<AtsBaseSport> list = atsBaseSportService.selectAll(atsBaseSport, page);

        int count = atsBaseSportService.selectCount(atsBaseSport);

        map.put("code", 0);
        map.put("msg", "");
        map.put("count", count);
        map.put("data", list);
        return map;

    }

    /**
     * 增加或者修改
     *
     * @param atsBaseSport
     * @return
     */
    @ResponseBody
    @RequestMapping("saveOrUpdate")

    public Message saveOrUpdate(AtsBaseSport atsBaseSport, HttpServletRequest request) {
        Message mg = new Message();

        int rows = 0;
        if (atsBaseSport.getSportId() != null) {//修改
            rows = atsBaseSportService.updateByPrimaryKeySelective(atsBaseSport,request);
        } else {//增加
            Date date = new Date();
            atsBaseSport.setCreatTime(date);
            atsBaseSport.setIsva(1);
            rows = atsBaseSportService.insertSelective(atsBaseSport,request);
        }
        if (rows > 0) {
            mg.setNum(1);
            mg.setMsg("操作成功");
        } else {
            mg.setNum(0);
            mg.setMsg("操作失败");
        }
        return mg;
    }

    /**
     * 逻辑删除
     *
     * @param atsBaseSport
     * @return
     */
    @ResponseBody
    @RequestMapping("updateIsva")
    public Message updateIsva(AtsBaseSport atsBaseSport) {
        Message message = new Message();
        atsBaseSport.setIsva(0);
        atsBaseSportService.updateIsva(atsBaseSport);
        message.setNum(1);
        message.setMsg("删除成功");
        return message;

    }

    /**
     * 根据id查
     *
     * @param sportId
     * @return
     */
    @ResponseBody
    @RequestMapping("selectByPrimaryKey")
    public Map<String,Object> selectByPrimaryKey(Integer sportId) {
        Map<String,Object> map = new HashMap<>();
        AtsBaseSport ats = atsBaseSportService.selectByPrimaryKey(sportId);
        List<AtsBaseSportItem> list = atsBaseSportItemService.selectByPrimaryKey2(sportId);
        String one = "";
        String two = "";
        for(int i = 0;i<list.size();i++) {
            AtsBaseSportItem tr = list.get(i);
            AtsBaseItem ats1 = atsBaseItemMapper.selectByPrimaryKey(tr.getItemId());
            if(i == list.size()-1) {
                one+=tr.getItemId();
                two+=ats1.getItemExp();
            }else {
                one+=tr.getItemId()+",";
                two+=ats1.getItemExp()+",";
            }
        }

        map.put("two",two);
        map.put("one",one);
        map.put("ats",ats);
        return map;

    }






    /**
     * 批量删除
     *
     * @param ids
     * @return
     */
    @ResponseBody
    @RequestMapping("deleteAll")
    public Message deleteAll(String ids) {
        Message message = new Message();
        System.out.println("id" + ids);
        String id[] = ids.split(",");
        int rows = 0;
        for (int i = 0; i < id.length; i++) {
            AtsBaseSport atsBaseSport = new AtsBaseSport();
            atsBaseSport.setSportId(Integer.valueOf(id[i]));
            atsBaseSport.setIsva(0);

            atsBaseSportService.updateIsva(atsBaseSport);
            rows++;
        }
        message.setNum(1);
        message.setMsg("恭喜,删除成功!共删除" + rows + "行记录!");
        return message;
    }

    /**
     * 查询所有
     *
     * @return
     */
    @RequestMapping("findAll")
    @ResponseBody
    public List<AtsBaseSport> findAll() {

        List<AtsBaseSport> list = atsBaseSportService.findAll();
        return list;
    }


}
